﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ERPStore.Offers.Models
{
	public static class ModelConverter
	{
		internal static OfferedProduct ToOfferedProductModel(this Repositories.Datas.OfferedProductData data)
		{
			var model = new OfferedProduct();
			model.Id = data.Id;
			model.ProductId = data.ProductId;
			model.OfferId = data.OfferId;
			if (data.Discount.HasValue)
			{
				model.Discount = data.Discount / 1000000.0;
			}
			if (data.SalePrice.HasValue)
			{
				model.SalePrice = data.SalePrice / 1000000.0m;
			}
			if (data.ShowStrikedPrice.HasValue)
			{
				model.ShowStrikedPrice = data.ShowStrikedPrice.GetValueOrDefault(false);
			}
			if (data.FixedShippingPrice.HasValue)
			{
				model.FixedShippingPrice = data.FixedShippingPrice / 1000000.0m;
			}
			return model;
		}

		internal static Repositories.Datas.OfferedProductData ToOfferedProductData(this Models.OfferedProduct model)
		{
			var data = new Repositories.Datas.OfferedProductData();
			if (model.Discount.HasValue)
			{
				data.Discount = Convert.ToInt64(model.Discount * 1000000);
			}
			if (model.FixedShippingPrice.HasValue)
			{
				data.FixedShippingPrice = Convert.ToInt64(model.FixedShippingPrice * 1000000);
			}
			if (model.SaleFactor.HasValue)
			{
				data.SaleFactor = Convert.ToInt64(model.SaleFactor * 1000000);
			}
			if (model.SalePrice.HasValue)
			{
				data.SalePrice = Convert.ToInt64(model.SalePrice * 1000000);
			}
			data.ShowStrikedPrice = model.ShowStrikedPrice;
			data.ProductId = model.ProductId;
			data.OfferId = model.OfferId;
			return data;
		}

		internal static OfferEx ToOfferModel(this Repositories.Datas.OfferData data)
		{
			var result = new OfferEx();
			result.CSSTag = data.CSSTag;
			result.Description = data.Description;
			result.EndDate = data.EndDate;
			result.Id = data.Id;
			result.Link = data.Link;
			result.Name = data.Name;
			result.StartDate = data.StartDate;
			result.Title = data.Title;
			result.ViewName = data.ViewName;
			result.PhysicalStockMandatory = data.PhysicalStockMandatory;
			result.ProductViewName = data.ProductViewName;
			return result;
		}

		internal static Repositories.Datas.OfferData ToOfferData(this OfferEx model)
		{
			var result = new Repositories.Datas.OfferData();
			result.CSSTag = model.CSSTag;
			result.Description = model.Description;
			result.EndDate = model.EndDate;
			result.Id = model.Id;
			result.Link = model.Link;
			result.Name = model.Name;
			result.StartDate = model.StartDate;
			result.Title = model.Title;
			result.ViewName = model.ViewName;
			result.PhysicalStockMandatory = model.PhysicalStockMandatory;
			result.ProductViewName = model.ProductViewName;
			return result;
		}
	}
}
